package com.ct;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.StaticLog;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@SpringBootTest
public class TreeDemo {

    @Test
    public void a1() {
        // 构建node列表
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();

        nodeList.add(new TreeNode<>("1", "0", "系统管理", 5));
        nodeList.add(new TreeNode<>("11", "1", "用户管理", 222222));
        nodeList.add(new TreeNode<>("111", "11", "用户添加", 0));
        nodeList.add(new TreeNode<>("2", "0", "店铺管理", 1));
        nodeList.add(new TreeNode<>("21", "2", "商品管理", 44));
        nodeList.add(new TreeNode<>("221", "2", "商品管理2", 2));

        // 0表示最顶层的id是0
        List<Tree<String>> treeList = TreeUtil.build(nodeList, "0");

        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都有默认值的
        treeNodeConfig.setWeightKey("order");
        treeNodeConfig.setIdKey("rid");
        // 最大递归深度
        treeNodeConfig.setDeep(3);
        //转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.<TreeNode, String>build(Collections.unmodifiableList(nodeList), "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId().toString());
                    tree.setParentId(treeNode.getParentId().toString());
                    tree.setWeight(treeNode.getWeight());
                    tree.setName(treeNode.getName());
                    // 扩展属性 ...
                    tree.putExtra("extraField", 666);
                    tree.putExtra("other", new Object());
                });

        StaticLog.info("nodeList->{}", nodeList);
        StaticLog.info("nodeList->{}", JSONUtil.toJsonPrettyStr(nodeList));
    }

    @Test
    public void a2() {
// 构建node列表
        List<Map> nodeList = CollUtil.newArrayList();
        nodeList.add(MapUtil.builder(new HashMap<String, String>()).put("id", "1").put("parentId", "0").put("name", "系统管理").put("weight", "1").build());
        nodeList.add(MapUtil.builder(new HashMap<String, String>()).put("id", "11").put("parentId", "1").put("name", "用户管理").put("weight", "2").build());
        nodeList.add(MapUtil.builder(new HashMap<String, String>()).put("id", "111").put("parentId", "11").put("name", "用户添加").put("weight", "3").build());
        nodeList.add(MapUtil.builder(new HashMap<String, String>()).put("id", "1111").put("parentId", "111").put("name", "测试").put("weight", "4").build());
        nodeList.add(MapUtil.builder(new HashMap<String, String>()).put("id", "2").put("parentId", "0").put("name", "店铺管理").put("weight", "5").build());
        nodeList.add(MapUtil.builder(new HashMap<String, String>()).put("id", "21").put("parentId", "2").put("name", "商品管理").put("weight", "6").build());
        nodeList.add(MapUtil.builder(new HashMap<String, String>()).put("id", "221").put("parentId", "2").put("name", "商品管理2").put("weight", "7").build());


        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都有默认值的
        treeNodeConfig.setIdKey("id1");
        treeNodeConfig.setParentIdKey("parentId1");
        treeNodeConfig.setNameKey("name1");
        treeNodeConfig.setWeightKey("sort");
        // 最大递归深度
        treeNodeConfig.setDeep(10);

        // 0表示最顶层的id是0
        List<Tree<Integer>> build = TreeUtil.build(nodeList, 0,treeNodeConfig, (node, tree) -> {
            tree.setId(MapUtil.getInt(node, "id"));
            tree.setParentId(MapUtil.getInt(node, "parentId"));
            tree.setName(MapUtil.getStr(node, "name"));
            tree.setWeight(MapUtil.getStr(node, "weight"));
            // 可自定义属性
            tree.putExtra("gg", "G600");
        });


        StaticLog.info("build->{}", JSONUtil.toJsonPrettyStr(build));


    }
}
